import 'package:enterprise_v6_app/app/components/common/no_permissions_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/drawer_title.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/modules/application/closure/closure_controller.dart';

/// 封闭化
class ClosurePage extends StatelessWidget {
  ClosurePage({super.key});

  final controller = Get.put(ClosureController());

  Widget _appBar(int? titleIndex) {
    if (controller.titleList[titleIndex!].pageTitle == null) {
      return Text(controller.titleList[titleIndex].expandTitle,
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w500,
          ));
    }
    return Container(
      height: 35.w,
      padding: EdgeInsets.only(left: 16.w, right: 16.w),
      child: Container(
        width: 84.w * controller.titleList[titleIndex].pageTitle!.length,
        decoration: BoxDecoration(
          color: const Color(0x1F5C6DFF),
          border: Border.all(
            color: const Color(0xff5C6DFF),
            width: 1.w,
          ),
          borderRadius: BorderRadius.circular(4.r),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: controller.titleList[titleIndex].pageTitle!.map((e) {
            final bool isCurrent = controller.titleList[titleIndex].pageTitle?.indexOf(e) == controller.titleList[titleIndex].currentPageTitle;
            return Container(
              width: 81.w,
              height: 28.w,
              decoration: BoxDecoration(
                color: isCurrent ? const Color(0xff5C6DFF) : null,
                borderRadius: BorderRadius.circular(3.r),
              ),
              alignment: Alignment.center,
              child: Text(
                e,
                style: TextStyle(fontSize: 14.sp, color: isCurrent ? Colors.white : const Color(0xff5C6DFF), fontWeight: FontWeight.w500),
              ),
            ).click(() {
              controller.selectTitle(titleIndex, controller.titleList[titleIndex].pageTitle!.indexOf(e));
            });
          }).toList(),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (ClosureController controller) {
        if (controller.titleList.isEmpty) {
          return const NoPermissionsWidget();
        }
        return Scaffold(
          appBar: CustomAppBar(
            title: _appBar(controller.selectIndex),
          ),
          body: Scaffold(
            key: controller.scaffoldKey,
            drawer: Drawer(
              width: 200.w,
              child: Container(
                color: Colors.white,
                child: Column(
                  children: [
                    DrawerTitle.titleDrawer('收起', AppImages.icon_launch).click(() {
                      controller.scaffoldKey.currentState?.closeDrawer();
                    }),
                    Column(
                      children: controller.titleList.map((e) {
                        return DrawerTitle.titleDrawer(e.title, e.icon, isSelected: controller.selectIndex == controller.titleList.indexOf(e)).click(() {
                          controller.selectExpandMenu(controller.titleList.indexOf(e));
                        });
                      }).toList(),
                    ),
                  ],
                ),
              ),
            ),
            body: Row(
              children: [
                Container(
                  width: 60.w,
                  color: Colors.white,
                  alignment: Alignment.topCenter,
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        DrawerTitle.title('展开', AppImages.icon_launch).click(() {
                          controller.scaffoldKey.currentState?.openDrawer();
                        }),
                        Column(
                          children: controller.titleList.map((e) {
                            return DrawerTitle.title(e.title, e.icon, isSelected: controller.selectIndex == controller.titleList.indexOf(e)).click(() {
                              controller.selectLeftMenu(controller.titleList.indexOf(e));
                            });
                          }).toList(),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: controller.titleList[controller.selectIndex].pages[controller.titleList[controller.selectIndex].currentPageTitle],
                ),
              ],
            ),

          ),
        );
      },
    );
  }
}
